import { useReducer } from 'react';

/** 定义初始状态信息  */
interface IstateType {
  // procureApplyRow: Record<string, any>;
  counterpartRows: Record<string, any>[];
  contractTempRow: Record<string, any>;
  contarctJsonList: Record<string, any>;
  materialCheckRows: Record<string, any>[];
  partyFirstSelectRows: Record<string, any>[]
}

/** reducer 枚举类型 */
enum IContractsActionEnum {
  // SELECT_PROCUREAPPLY = 'select_procureApply',
  SELECT_COUNTERPARTROWS = 'select_counterpartRows',
  SELECT_COUNTERTEMPLA = 'select_contractTempal',
  CONTRACT_JSONLIST = 'contract_jsonList',
  SELECT_MATERIALLIST = 'select_materialCheckRows',
  SELECT_PARTYALIST = "select_partyAList"
}

/**
 * 定义页面操作匹配
 * @type 操作类型
 * @payload 传递数据
 */
interface IActionType {
  type: IContractsActionEnum;
  payload?: any;
}

/** 定义供应商状态管理 */
const useContractsReducer = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    // procureApplyRow: {},
    counterpartRows: [],
    contractTempRow: {},
    contarctJsonList: {},
    materialCheckRows: [],
    partyFirstSelectRows: [],
  };

  /** 状态管理逻辑处理 */
  const updateReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      /** 选择申请 */
      // case IContractsActionEnum.SELECT_PROCUREAPPLY:
      //   return {
      //     ...state,
      //     procureApplyRow: action.payload,
      //   };

      /** 选择相对方 */
      case IContractsActionEnum.SELECT_COUNTERPARTROWS:
        return {
          ...state,
          counterpartRows: action.payload,
        };

      /** 合同范本 */
      case IContractsActionEnum.SELECT_COUNTERTEMPLA:
        return {
          ...state,
          contractTempRow: action.payload,
        };

      /** 处理列表数据 */
      case IContractsActionEnum.CONTRACT_JSONLIST:
        return {
          ...state,
          contarctJsonList: action.payload,
        };

      // 供货清单
      case IContractsActionEnum.SELECT_MATERIALLIST:
        return {
          ...state,
          materialCheckRows: action.payload,
        };

      // /** 合同主体 */
      case IContractsActionEnum.SELECT_PARTYALIST:
        return {
          ...state,
          partyFirstSelectRows: action.payload,
        }

      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, contractDispatch] = useReducer(updateReducer, initState);

  return {
    ...state,
    contractDispatch,
  };
};

export { useContractsReducer, IContractsActionEnum };
